package priority;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GaoBo
 * Date: 2021-07-23
 * Time: 9:09
 */
/*class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }
}*/
class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        //是不是两个引用 同时指向一个对象
        if (this == o) return true;
        //如果传进来是一个null
        if (o == null) return false;

        Student student = (Student) o;
        //
        return age == student.age &&
                Objects.equals(name, student.name);
    }
    //hashmap讲完后
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
class AgeComparator implements Comparator<Student> {


    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge()-o2.getAge();
    }
}

class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
}
public class TestDemo1 {

    public static void main(String[] args) {
        Student student1 = new Student("bit",1);
        Student student2 = new Student("bit",1);

        System.out.println(student1 == student2);

        System.out.println(student1.equals(student2));

    }


    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {

        PriorityQueue< List<Integer>> maxHeap = new PriorityQueue<>(k, new Comparator<List<Integer>>() {
            @Override
            public int compare(List<Integer> o1, List<Integer> o2) {
                //return o2-o1;
                return (o2.get(0)+o2.get(1)) - (o1.get(0)+o2.get(1));
            }
        });

        //nums1: [1,2]     nums2: [1 , 2, 3]  -> [1,1]   1,2    1,3   2,1 2,2  2,3
        //nums1: [1,2,3]     nums2: [1]
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {

                if(maxHeap.size() < k) {
                    List<Integer> pair = new ArrayList<>();
                    pair.add(nums1[i]);
                    pair.add(nums2[j]);
                    maxHeap.offer(pair);
                }else {
                    List<Integer> top = maxHeap.peek();
                    //如果当前元素 比 堆顶小  就弹出堆顶 然后将小的数据放入堆
                    int topValue = top.get(0)+top.get(1);

                    if(topValue > nums1[i] + nums2[j]) {
                        maxHeap.poll();
                        List<Integer> pair = new ArrayList<>();
                        pair.add(nums1[i]);
                        pair.add(nums2[j]);
                        maxHeap.offer(pair);
                    }
                }
            }
        }

        List<List<Integer>> ret = new ArrayList<>();
        //堆当中放的就是
        for (int i = 0; i < k && !maxHeap.isEmpty() ; i++) {
            ret.add(maxHeap.poll());
        }
        return ret;
    }


    public static void topk(int[] array,int k) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        //大堆
        for (int i = 0; i < array.length; i++) {
            if(maxHeap.size() < k) {
                maxHeap.offer(array[i]);
            }else {
                int top = maxHeap.peek();
                if(top > array[i]) {
                    maxHeap.poll();
                    maxHeap.offer(array[i]);
                }
            }
        }
        System.out.println(maxHeap);
    }


    public static void main3(String[] args) {
        int[] array = {1,31,2,10,7,35,21,19,56};//找到前3个最小的数据

        topk(array,3);

        System.out.println("===========");
        //Arrays.sort(array);
    }

    public static void main1(String[] args) {

        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(new AgeComparator());

        priorityQueue.offer(new Student("bit",18));

        priorityQueue.offer(new Student("gaobo",8));

        System.out.println(priorityQueue);

    }

    public static void main2(String[] args) {
        Student[] students = new Student[2];
        students[0] = new Student("gaobo",12);
        students[1] = new Student("bit",89);
        Arrays.sort(students,new NameComparator());
        System.out.println(Arrays.toString(students));
    }


}
